package cn.hotapk.lwdemov2;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import cn.hotapk.fastandrutils.utils.FPermissionUtils;
import cn.hotapk.lewansdk.LWSdk;
import cn.hotapk.lewansdk.bean.RoleBean;
import cn.hotapk.lewansdk.listener.LWGlobalListener;


public class MainActivity extends Activity {

    private RelativeLayout login_lay, enter_lay, user_lay, game_lay;
    private Button login_bt, back_bt, enter_bt, start_bt, pay_bt;
    private LWSdk lwSdk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        FPermissionUtils.requestPermissions(this, 200, new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, new FPermissionUtils.OnPermissionListener() {
            @Override
            public void onPermissionGranted() {
                initSdk();
            }

            @Override
            public void onPermissionDenied(String[] deniedPermissions) {

            }

            @Override
            public void manifestUnPermission(String[] unpermission) {

            }
        });

    }

    /**
     * 初始化sdk（必须在获取权限之后初始化）
     */
    private void initSdk() {
        lwSdk = LWSdk.creatSdk(MainActivity.this);
        lwSdk.regGlobalListener(lwGlobalListener);
    }

    private void initView() {
        login_lay = findViewById(R.id.login_lay);
        enter_lay = findViewById(R.id.enter_lay);
        user_lay = findViewById(R.id.user_lay);
        game_lay = findViewById(R.id.game_lay);

        login_bt = findViewById(R.id.login_bt);
        back_bt = findViewById(R.id.back_bt);
        enter_bt = findViewById(R.id.enter_bt);
        start_bt = findViewById(R.id.start_bt);
        pay_bt = findViewById(R.id.pay_bt);


        enter_lay.setVisibility(View.GONE);
        user_lay.setVisibility(View.GONE);
        game_lay.setVisibility(View.GONE);

        //登录
        login_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lwSdk.login();
            }
        });

        //进入选择区
        enter_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enter_lay.setVisibility(View.GONE);
                login_lay.setVisibility(View.GONE);
                user_lay.setVisibility(View.VISIBLE);
                game_lay.setVisibility(View.GONE);
            }
        });

        //选择区返回登录
        back_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login_lay.setVisibility(View.VISIBLE);
                enter_lay.setVisibility(View.GONE);
                user_lay.setVisibility(View.GONE);
                game_lay.setVisibility(View.GONE);
                lwSdk.logout();
            }
        });

        start_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login_lay.setVisibility(View.GONE);
                enter_lay.setVisibility(View.GONE);
                user_lay.setVisibility(View.GONE);
                game_lay.setVisibility(View.VISIBLE);
                //角色上报
                RoleBean roleBean = new RoleBean(RoleBean.ROLE_CREAT, "11", "德维尔德", "11", "的我", "13", "111", "的味道");
                lwSdk.reportRole(roleBean);
            }
        });

        //支付
        pay_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    lwSdk.pay(200, "的的我", "33333");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        FPermissionUtils.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        lwSdk.exit();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 全局回调
     */
    LWGlobalListener lwGlobalListener = new LWGlobalListener() {
        @Override
        public void initIng() {
        }

        //初始化成功
        @Override
        public void initSuccess() {

        }

        @Override
        public void initFail(int status, String msg) {

        }

        //登录成功
        @Override
        public void loginSuccess(String userName, String token) {
            login_lay.setVisibility(View.GONE);
            enter_lay.setVisibility(View.VISIBLE);
            user_lay.setVisibility(View.GONE);
            game_lay.setVisibility(View.GONE);

        }

        @Override
        public void loginFail(int status, String msg) {

        }

        //注销账号
        @Override
        public void cancelAccount() {
            login_lay.setVisibility(View.VISIBLE);
            enter_lay.setVisibility(View.GONE);
            user_lay.setVisibility(View.GONE);
            game_lay.setVisibility(View.GONE);
        }

        //修改密码成功
        @Override
        public void changePasswSuccess() {
            login_lay.setVisibility(View.VISIBLE);
            enter_lay.setVisibility(View.GONE);
            user_lay.setVisibility(View.GONE);
            game_lay.setVisibility(View.GONE);
        }

        //更新角色成功
        @Override
        public void UpRoleSuccess() {

        }

        @Override
        public void UpRoleFail(int status, String msg) {

        }

        //支付成功
        @Override
        public void paySuccess() {

        }

        @Override
        public void payFail(int status, String msg) {

        }

        //取消支付
        @Override
        public void payCancel() {

        }
    };


}
